1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package sun.text;
27
28 public final class UCompactIntArray implements Cloneable {
29
30
31
32
33 public UCompactIntArray() {
34 values = new int[16][];
35 indices = new short[16][];
36 blockTouched = new boolean[16][];
37 planeTouched = new boolean[16];
38 }
39
40 public UCompactIntArray(int defaultValue) {
41 this();
42 this.defaultValue = defaultValue;
43 }
44
45
46
47
48
49
50 public int elementAt(int index) {
51 int plane = (index & PLANEMASK) >> PLANESHIFT;
52 if (!planeTouched[plane]) {
53 return defaultValue;
54 }
55 index &= CODEPOINTMASK;
56 return values[plane][(indices[plane][index >> BLOCKSHIFT] & 0xFFFF)
57 + (index & BLOCKMASK)];
58 }
59
60
61
62
63
64
65
66
67 public void setElementAt(int index, int value) {
68 if (isCompact) {
69 expand();
70 }
71 int plane = (index & PLANEMASK) >> PLANESHIFT;
72 if (!planeTouched[plane]) {
73 initPlane(plane);
74 }
75 index &= CODEPOINTMASK;
76 values[plane][index] = value;
77 blockTouched[plane][index >> BLOCKSHIFT] = true;
78 }
79
80
81
82
83
84 public void compact() {
85 if (isCompact) {
86 return;
87 }
88 for (int plane = 0; plane < PLANECOUNT; plane++) {
89 if (!planeTouched[plane]) {
90 continue;
91 }
92 int limitCompacted = 0;
93 int iBlockStart = 0;
94 short iUntouched = -1;
95
96 for (int i = 0; i < indices[plane].length; ++i, iBlockStart += BLOCKCOUNT) {
97 indices[plane][i] = -1;
98 if (!blockTouched[plane][i] && iUntouched != -1) {
99
100
101
102 indices[plane][i] = iUntouched;
103 } else {
104 int jBlockStart = limitCompacted * BLOCKCOUNT;
105 if (i > limitCompacted) {
106 System.arraycopy(values[plane], iBlockStart,
107 values[plane], jBlockStart, BLOCKCOUNT);
108 }
109 if (!blockTouched[plane][i]) {
110
111 iUntouched = (short)jBlockStart;
112 }
113 indices[plane][i] = (short)jBlockStart;
114 limitCompacted++;
115 }
116 }
117
118
119 int newSize = limitCompacted * BLOCKCOUNT;
120 int[] result = new int[newSize];
121 System.arraycopy(values[plane], 0, result, 0, newSize);
122 values[plane] = result;
123 blockTouched[plane] = null;
124 }
125 isCompact = true;
126 }
127
128
129
130
131
132
133
134
135 private void expand() {
136 int i;
137 if (isCompact) {
138 int[] tempArray;
139 for (int plane = 0; plane < PLANECOUNT; plane++) {
140 if (!planeTouched[plane]) {
141 continue;
142 }
143 blockTouched[plane] = new boolean[INDEXCOUNT];
144 tempArray = new int[UNICODECOUNT];
145 for (i = 0; i < UNICODECOUNT; ++i) {
146 tempArray[i] = values[plane][indices[plane][i >> BLOCKSHIFT]
147 & 0xffff + (i & BLOCKMASK)];
148 blockTouched[plane][i >> BLOCKSHIFT] = true;
149 }
150 for (i = 0; i < INDEXCOUNT; ++i) {
151 indices[plane][i] = (short)(i<<BLOCKSHIFT);
152 }
153 values[plane] = tempArray;
154 }
155 isCompact = false;
156 }
157 }
158
159 private void initPlane(int plane) {
160 values[plane] = new int[UNICODECOUNT];
161 indices[plane] = new short[INDEXCOUNT];
162 blockTouched[plane] = new boolean[INDEXCOUNT];
163 planeTouched[plane] = true;
164
165 if (planeTouched[0] && plane != 0) {
166 System.arraycopy(indices[0], 0, indices[plane], 0, INDEXCOUNT);
167 } else {
168 for (int i = 0; i < INDEXCOUNT; ++i) {
169 indices[plane][i] = (short)(i<<BLOCKSHIFT);
170 }
171 }
172 for (int i = 0; i < UNICODECOUNT; ++i) {
173 values[plane][i] = defaultValue;
174 }
175 }
176
177 public int getKSize() {
178 int size = 0;
179 for (int plane = 0; plane < PLANECOUNT; plane++) {
180 if (planeTouched[plane]) {
181 size += (values[plane].length * 4 + indices[plane].length * 2);
182 }
183 }
184 return size / 1024;
185 }
186
187 private static final int PLANEMASK = 0x30000;
188 private static final int PLANESHIFT = 16;
189 private static final int PLANECOUNT = 0x10;
190 private static final int CODEPOINTMASK = 0xffff;
191
192 private static final int UNICODECOUNT = 0x10000;
193 private static final int BLOCKSHIFT = 7;
194 private static final int BLOCKCOUNT = (1<<BLOCKSHIFT);
195 private static final int INDEXSHIFT = (16-BLOCKSHIFT);
196 private static final int INDEXCOUNT = (1<<INDEXSHIFT);
197 private static final int BLOCKMASK = BLOCKCOUNT - 1;
198
199 private int defaultValue;
200 private int values[][];
201 private short indices[][];
202 private boolean isCompact;
203 private boolean[][] blockTouched;
204 private boolean[] planeTouched;
205 };